package com.danciyixue.boot.client.testRecord.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.client.test.model.PreTestSaveRequest;
import com.danciyixue.boot.client.testRecord.model.TestRecordSaveRequest;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.core.security.util.SecurityUtils;
import com.danciyixue.boot.dcyx.model.query.TestAnswersQuery;
import com.danciyixue.boot.dcyx.model.query.TestRecordsQuery;
import com.danciyixue.boot.dcyx.model.vo.TestAnswersVO;
import com.danciyixue.boot.dcyx.model.vo.TestRecordsVO;
import com.danciyixue.boot.dcyx.service.TestAnswersService;
import com.danciyixue.boot.dcyx.service.TestRecordsService;

import java.util.List;
import com.danciyixue.boot.dcyx.service.UnitTestRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端学生测试记录控制器
 */
@Tag(name = "20. 测试记录", description = "测试记录")
@RestController
@RequestMapping("/api/client/v1/test-record")
@RequiredArgsConstructor
public class DcyxTestRecordController {

    private final TestRecordsService testRecordsService;
    private final UnitTestRecordsService unitTestRecordsService;

    /**
     * 获取测试答案数据
     */
    @GetMapping("/page")
    public PageResult<TestRecordsVO> page(TestRecordsQuery queryParams) {
        IPage<TestRecordsVO> page = testRecordsService.getTestRecordsPage(queryParams);
        return PageResult.success(page);
    }

    /**
     * 保存学前测试记录
     */
    @Operation(summary = "保存学前测试记录")
    @PostMapping("/pre-test/save")
    public Result<Void> savePreTestRecord(@RequestBody @Valid PreTestSaveRequest request) {
        boolean result = testRecordsService.savePreTestRecord(request);
        return Result.judge(result);
    }

    /**
     * 通过 program Id 查询测试记录 VO
     * 返回测试记录 VO 对象，详情信息,
     */
    @Operation(summary = "是否完成学前测试")
    @GetMapping("/pre-test/complete")
    public Result<TestRecordsVO> hasCompletedPreTest(
            @Parameter(description = "教材ID", required = true)
            @RequestParam Long programId
    ) {
//        Long userId = SecurityUtils.getUserId();
        TestRecordsVO result = testRecordsService.getTestRecordsByProgramId(programId);
        return Result.success(result);
    }

    /**
     * 保存测试记录信息
     */
    @Operation(summary = "保存测试记录信息")
    @PostMapping("/unit-test/save")
    public Result<Void> saveTestRecord(@RequestBody @Valid TestRecordSaveRequest request) {
        boolean result = testRecordsService.saveTestRecord(request);
        return Result.judge(result);
    }

    /**
     * 根据教材ID查询测试记录列表
     */
    @Operation(summary = "根据教材ID查询测试记录列表")
    @GetMapping("/list")
    public Result<List<TestRecordsVO>> getTestRecordsList(
            @Parameter(description = "教材ID", required = true)
            @RequestParam Long programId
    ) {
        List<TestRecordsVO> result = testRecordsService.getTestRecordsListByProgramId(programId);
        return Result.success(result);
    }

}